package com.test.personalblogproject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration

public class MailConfig {
    private static final String USERNAME="2145507446@qq.com";  // 指定发送邮件的账户
    private static final String PASSWORD="yamdmfxdaweochhj";  //指定发送邮箱的授权码

    /**
     * 配置MailSender
     * MailSender是一个接口，其一个实现类为JavaMailSenderImpl
     * @return
     */
    @Bean
    public MailSender mailSender(){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.qq.com");  //指定用来发送邮件服务器的主机名
        mailSender.setPort(465);    //默认端口，标准的SMTP端口
        mailSender.setUsername(USERNAME);  //配置自己的qq邮箱
        mailSender.setPassword(PASSWORD);   //配置自己的qq邮箱发送授权码

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.socketFactory.port", "465");

        return mailSender;

    }

}
